<?php

namespace zhtfrontend\models\datas;

use common\components\extenders\MenuManage;
use common\models\datas\Brand;
use common\models\datas\Cate;
use common\models\datas\Type;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%coupons}}".
 *
 * @property integer $id
 * @property string $name
 * @property string $code
 * @property string $type
 * @property string $description
 * @property integer $amount
 * @property integer $total
 * @property string $platforms
 * @property integer $quantity
 * @property integer $start_time
 * @property integer $end_time
 * @property string $conditions
 * @property integer $issue_qty
 * @property integer $created_at
 */
class Coupons extends \zhtfrontend\components\bases\BaseModel
{
    public $maturity_week;
    public $maturity_day;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%coupons}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
//            [['amount', 'total', 'quantity','issue_qty'], 'integer'],
            [['amount', 'total', 'quantity','maturity_value','maturity_day','maturity_week'], 'integer'],
//            [['name','amount', 'total', 'quantity','end_time', 'start_time', 'platforms', 'conditions'], 'required'],
            [['name','amount', 'total', 'quantity','end_time', 'start_time','maturity_type'], 'required'],
            [[ 'end_time', 'start_time','maturity_type'], 'string'],
            [[ 'end_time'], 'date_check'],
            [['type'], 'required','on'=>['updatecoupons','createcoupons']],
            [['name','code'], 'string', 'max' => 100],
            [['type'], 'string', 'max' => 20],
            [['maturity_week'],'required','when'=>function(){
                return $this->maturity_type == 'W';
            }],
            [['maturity_day'],'required','when'=>function(){
                return $this->maturity_type == 'D';
            }],
//            [['description', 'conditions'], 'string', 'max' => 255]
            [['description'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增id'),
            'name' => Yii::t('zhtfrontend', '名称'),
            'code' => Yii::t('zhtfrontend', '券码'),
            'type' => Yii::t('zhtfrontend', '类型'),
            'description' => Yii::t('zhtfrontend', '描述'),
            'amount' => Yii::t('zhtfrontend', '优惠金额'),
            'total' => Yii::t('zhtfrontend', '使用额度'),
            'platforms' => Yii::t('zhtfrontend', '平台限制'),
            'quantity' => Yii::t('zhtfrontend', '发放数量'),
            'start_time' => Yii::t('zhtfrontend', '开始时间'),
            'end_time' => Yii::t('zhtfrontend', '结束时间'),
            'conditions' => Yii::t('zhtfrontend', '使用条件'),
            'issue_qty' => Yii::t('zhtfrontend', '领取数量'),
            'created_at' => Yii::t('zhtfrontend', '创建时间'),
            'maturity_type' => Yii::t('zhtfrontend', '用户使用失效类型'),
            'maturity_value' => Yii::t('zhtfrontend', '用户使用失效值'),
            'maturity_day' => Yii::t('zhtfrontend', '用户使用失效天数'),
            'maturity_week' => Yii::t('zhtfrontend', '用户使用失效周'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['platforms'] = function () {
            $string='';
            $platforms=unserialize($this->platforms);
            if(!empty($platforms)){
                foreach($platforms as $k=>$v){
                    $string==''?$string.=$v:$string.='|'.$v;
                }
            }
            return $string;
        };
        $fields['conditions'] = function () {
            $data=unserialize($this->conditions);
            $conditions=$data['key'];
            switch($conditions){
                case 'all':
                    return \Yii::t('zhtfrontend', '所有商品');
                case 'type':
                    $type_name=Type::find()->select('name')->where(['in','id',$data['value']])->column();
                    return \Yii::t('zhtfrontend', '指定类型('.implode('|',$type_name).')');
                case 'cate':
                    $cate_name=Cate::find()->select('name')->where(['in','id',$data['value']])->column();
                    return \Yii::t('zhtfrontend', '指定分类('.implode('|',$cate_name).')');
                case 'brand':
                    $brand_name=Brand::find()->select('name')->where(['in','id',$data['value']])->column();
                    return \Yii::t('zhtfrontend', '指定品牌('.implode('|',$brand_name).')');
            }
        };
        $fields['start_time'] = function () {
            return \Yii::$app->formatter->asDatetime($this->start_time);
        };
        $fields['end_time'] = function () {
            return \Yii::$app->formatter->asDatetime($this->end_time);
        };
        $fields['amount'] = function () {
            switch ($this->type){
                case 'discount':
                    return ($this->amount)/10 . '折券';
                default:
                    return $this->amount;
            }
        };
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['type'] = function (){
            switch ($this->type){
                case 'full':
                    return \Yii::t('zhtfrontend','满减券');
                case 'freight';
                    return \Yii::t('zhtfrontend','免运费券');
                case 'discount':
                    return \Yii::t('zhtfrontend','折扣券');
            }
        };

        return $fields;
    }

    public function extraFields()
    {
        return [
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
        ];
    }

    public function date_check($attribute)
    {
        if (isset($this->start_time) && strtotime($this->start_time)>=strtotime($this->$attribute)) {
            $this->addError($attribute, Yii::t('zhtfrontend', '结束时间必须大于开始时间'));
        }
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
        ]);
    }
}
